WRANGLERS

Commonwealth War Dead

Separate a column

Photo by Razi Purjafarian on Unsplash

Photo by Razi Purjafarian on Unsplash

Never think that war, no matter how necessary, nor how justified, is not a crime.
— Ernest Hemingway


Ingest the data

Surname, forename, DateOfDeath

url_root <- "https://raw.githubusercontent.com/UN-AVT/kamino-source/main/sources/0-shared/data/"
url_file <- "commonwealth-war-dead/casualty-search.csv"
url <- paste0(url_root, url_file)

df <- read.csv(url, header = TRUE, stringsAsFactors = FALSE)
df

Wrangle

Separate a column into multiple columns using separate()

#Selecting only certain columns
df1 <- df %>% select(Surname,Forename,DateOfDeath)

#splitting dateofdeath column into day,month, and year columns
df2 <- df1%>%separate(DateOfDeath, c('day','month','year'),"/")
df2

Another useful function to split a column into separate columns is str_split_fixed() in stringr package

#splitting Forename into First name and middle name
df4 <- df2
new_df <- as.data.frame(str_split_fixed(df4$Forename," ",2))
colnames(new_df) <- c("First Name","Middle Name")
df4 <- cbind(df4,new_df)
df4 <- df4[,c("Surname","First Name","Middle Name")]
df4

References

Citations and data sources

  • Narration and data: Walk Free, GO